﻿#include "widget.h"
#include "ui_widget.h"
#include <QTimer>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QTime>
#include <QDebug>

#include "../../CatTools/ImageTools/ImageToolFactory.h"
#include "../../CatTools/ImageTools/ImageToolBase.h"

#include "../../CatTools/TeachingTools/AbsTeachingTool.h"
#include "../../CatTools/TeachingTools/TeachingToolRuler.h"
#include "../../CatTools/TeachingTools/TeachingToolCompass.h"
#include "../../CatTools/TeachingTools/TeachingToolProtractor.h"
#include "../../CatTools/TeachingTools/TeachingToolTrangle.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    m_lTeachingToolRuler.clear();
    ui->setupUi(this);
    qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime()));
    scene = new QGraphicsScene;

    scene->setBackgroundBrush(QBrush(QColor("#244242")));
    scene->setSceneRect(-5000, -5000, 10000, 10000);
    QGraphicsView *view = new QGraphicsView(scene, this);

    ui->verticalLayout->addWidget(view);
}

Widget::~Widget()
{
    delete ui;
    //ui->graphicsView->
}


void Widget::on_ScreenShot_clicked()
{
    this->hide();
    QTimer *delay = new QTimer(this);
    connect(delay, &QTimer::timeout, this, [=](){
        ImageToolFactory factory;
        ImageToolBase* base = factory.NewTool(ImageToolFactory::TOOLS::SCREENSHOT);
        connect(base, &ImageToolBase::clicked, this, [=](ImageToolBase::STATE state){
            if(state == ImageToolBase::STATE::CLOSE){
                this->show();
                base->deleteLater();
            }
        });
        base->show();
        delay->deleteLater();
    });

    delay->start(300);
}

void Widget::on_MagifyIngGlass_clicked()
{
    this->hide();
    QTimer *delay = new QTimer(this);
    connect(delay, &QTimer::timeout, this, [=](){
        ImageToolFactory factory;
        ImageToolBase* base = factory.NewTool(ImageToolFactory::TOOLS::MAGNIFYINGGLASS);
        connect(base, &ImageToolBase::clicked, this, [=](ImageToolBase::STATE state){
            if(state == ImageToolBase::STATE::CLOSE){
                this->show();
                base->deleteLater();
            }
        });
        base->show();
        delay->deleteLater();
    });

    delay->start(300);
}

void Widget::on_pushButton_3_clicked()
{
    this->hide();
    QTimer *delay = new QTimer(this);
    connect(delay, &QTimer::timeout, this, [=](){
        ImageToolFactory factory;
        ImageToolBase* base = factory.NewTool(ImageToolFactory::TOOLS::SPOTLIGHT);
        connect(base, &ImageToolBase::clicked, this, [=](ImageToolBase::STATE state){
            if(state == ImageToolBase::STATE::CLOSE){
                this->show();
                base->deleteLater();
            }
        });
        base->show();
        delay->deleteLater();
    });

    delay->start(300);
}

void Widget::on_Ruler_clicked()
{
    TeachingToolRuler *m_pTeachingToolRuler = new TeachingToolRuler;
    m_lTeachingToolRuler.push_back(m_pTeachingToolRuler);
    //m_pTeachingToolRuler->SetPaintState();
    QObject::connect(m_pTeachingToolRuler, &TeachingToolRuler::Closeed, [=](){
        m_lTeachingToolRuler.removeOne(m_pTeachingToolRuler);
        scene->removeItem(m_pTeachingToolRuler);
        qDebug() << "QTeachingToolRuler close.";
    });
    QObject::connect(m_pTeachingToolRuler, &TeachingToolRuler::LineStartPos, [=](QPointF pos){
        qDebug() << "LineStartPos: " << pos;
    });
    QObject::connect(m_pTeachingToolRuler, &TeachingToolRuler::LineUpdatePos, [=](QPointF pos){
        qDebug() << "LineUpdatePos: " << pos;
    });
    scene->addItem(m_pTeachingToolRuler);
}

void Widget::on_openpant_clicked()
{
    for(auto temp : m_lTeachingToolTrangle)
    {
        temp->SetPenState(1);
    }
    for(auto temp : m_lTeachingToolRuler)
    {
        temp->SetPenState(1);
    }
}

void Widget::on_closepant_clicked()
{
    for(auto temp : m_lTeachingToolRuler)
    {
        temp->SetState(AbsTeachingTool::TEAHINGTOOL_STATE_NONE);
    }
    for(auto temp : m_lTeachingToolTrangle)
    {
        temp->SetState(AbsTeachingTool::TEAHINGTOOL_STATE_NONE);
    }
}

void Widget::on_Compass_clicked()
{
    TeachingToolCompass *m_pTeachingToolCompass = new TeachingToolCompass;
    QObject::connect(m_pTeachingToolCompass, &TeachingToolCompass::Closeed, [=](){
        scene->removeItem(m_pTeachingToolCompass);
        qDebug() << "TeachingToolCompass close.";
    });
    QObject::connect(m_pTeachingToolCompass, &TeachingToolCompass::HoverEntered, [=](){
        qDebug() << "TeachingToolCompass HoverEntered.";
    });
    QObject::connect(m_pTeachingToolCompass, &TeachingToolCompass::HoverLeaveed, [=](){
        qDebug() << "TeachingToolCompass HoverLeaveed.";
    });
    QObject::connect(m_pTeachingToolCompass, &TeachingToolCompass::ArcStart, [=](QRectF rect, qreal startAngle){
        qDebug() << "TeachingToolCompass ArcStart: " << rect << " : " << startAngle;
    });
    QObject::connect(m_pTeachingToolCompass, &TeachingToolCompass::ArcUpdate, [=](qreal updateAngle){
        qDebug() << "TeachingToolCompass ArcUpdate: " << updateAngle;
    });
    QObject::connect(m_pTeachingToolCompass, &TeachingToolCompass::ArcEnd, [=](){
        qDebug() << "TeachingToolCompass ArcEnd.";
    });
    scene->addItem(m_pTeachingToolCompass);
}

void Widget::on_Protractor_clicked()
{
    TeachingToolProtractor *m_pTeachingToolProtractor = new TeachingToolProtractor;
    QObject::connect(m_pTeachingToolProtractor, &TeachingToolProtractor::Closeed, [=](){
        scene->removeItem(m_pTeachingToolProtractor);
        qDebug() << "TeachingToolProtractor close.";
    });
    scene->addItem(m_pTeachingToolProtractor);
}

void Widget::on_triangularrule_clicked()
{
    TeachingToolTrangle *m_pTeachingToolTrangle = new TeachingToolTrangle;
    QObject::connect(m_pTeachingToolTrangle, &TeachingToolTrangle::Closeed, [=](){
        m_lTeachingToolTrangle.removeOne(m_pTeachingToolTrangle);
        scene->removeItem(m_pTeachingToolTrangle);
        qDebug() << "TeachingToolTrangle close.";
    });
    QObject::connect(m_pTeachingToolTrangle, &TeachingToolTrangle::HoverEntered, [=](){

    });
    QObject::connect(m_pTeachingToolTrangle, &TeachingToolTrangle::HoverLeaveed, [=](){

    });
    QObject::connect(m_pTeachingToolTrangle, &TeachingToolTrangle::LineStartPos, [=](QPointF pos){
        qDebug() << "Trangle::StartPos: " << pos;
    });
    QObject::connect(m_pTeachingToolTrangle, &TeachingToolTrangle::LineUpdatePos, [=](QPointF pos){
        qDebug() << "Trangle::UpdatePos: " << pos;
    });
    m_lTeachingToolTrangle.push_back(m_pTeachingToolTrangle);
    scene->addItem(m_pTeachingToolTrangle);
}
